<?php

/** 
 * @package ShareThisBar Plugin for Joomla! 1.5
 * @subpackage Form Element Checkboxesimgsort
 * @version $Id: sharethisbar.php 3.5 2012-12-29 17:00:33Z Dusanka $
 * @author Dusanka Ilic
 * @copyright (C) 2012 - Dusanka Ilic, All rights reserved.
 * @authorEmail: gog27.mail@gmail.com
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html, see LICENSE.txt
**/

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * JFormFieldCheckboxesimgsort form field class for the ShareThisBar plugin.
 * Displays options as a list of check boxes with images which are sortable(mouse dragged).
 * Based on element checkboxes.
 * @author Dusanka Ilic
 * @extension  ShareThisBar Plugin for Joomla! 1.5
 * @since         3.5
 */

class JElementCheckboxesimgsort extends JElement
{       
        /**
	* Element name
	*
	* @access	protected
	* @var		string
	*/
	var	$_name = 'Checkboxesimgsort';

	/**
	 * Flag to tell the field to always be in multiple values mode.
	 *
	 * @var    boolean
	 */
	protected $forceMultiple = true;
	  
        function fetchElement($name, $value, &$node, $control_name)
	{       
                if (isset($value)) {
                    if (is_array($value)) {
                      $nizChecked = $value;
                    } else {
                      $nizChecked = (array) $value;
                    }
                } else {
                   $nizChecked = array();    
                }
                
                // Array optsIncluded contains all options for rendering in admin panel, first selected, and after them
                // those unselected.
		$optsIncluded = array();
	 
                // Only if there are checked options. For every checked option.
		if (count($nizChecked)) {
		
		  foreach($nizChecked as $elChecked)
		  {
			
                      // For all options in xml file. 
                      foreach ($node->children() as $option)
                      {
                          
			$val	= $option->attributes('value');
                        // Daj podatke za cekiranu opciju.
                        if ($val == $elChecked) {
                            $obj = new stdClass;
                            $obj->value	= $val;
                            $obj->showlabel = $option->attributes('showlabel');
                            $obj->simg = $option->attributes('simg');
                            $obj->simgalt = $option->attributes('simgalt');
                            $obj->simgtitle = $option->attributes('simgtitle');
                            $obj->text = $option->data();
    
                            $obj->checked = true;
                            
                            $optsIncluded[] = $obj;
                            break;
  
                        }
                        
                      }
            								
		   } //  end foreach($nizChecked as $elChecked)
		
		} //end if ($nizChecked) {
                
		foreach ($node->children() as $option)
		{
			$val	= $option->attributes('value');

                        // Is xml option in selected buttons? 
                        $inChecked = in_array((string) $val, (array) $nizChecked);
                        
                        // Only unchecked options.
                        if (!$inChecked) {
                            $obj = new stdClass;
                            $obj->value	= $val;
                            $obj->showlabel = $option->attributes('showlabel');
                            $obj->simg = $option->attributes('simg');
                            $obj->simgalt = $option->attributes('simgalt');
                            $obj->simgtitle = $option->attributes('simgtitle');
                            $obj->text = $option->data();
                            $obj->checked = false;
                            
                            $optsIncluded[] = $obj;
                        }
			
		}
                
                //*******************  HTML  ************************************
		// Initialize variables.
		$html = array();

                $class = ($node->attributes('class')) ? ' class="inputbox ' . (string) $node->attributes('class') . '"' : ' class="inputbox"';
                
                $id = $node->attributes('name');
                $name = $id;
                
		// Start the checkbox field output.
		$html[] = '<div id="' . $id . '"' . $class . '>';
		
		// Build the checkbox field output. 
		$html[] = '<ul  id="stbul">';
		
                if (count($optsIncluded)) {

                foreach ($optsIncluded as $i => $option)
		{
		
                    // Initialize some option attributes.
                    $checked = ($option->checked) ? ' checked="checked"' : '';
                    	
                    // img html element
                    $simg = (!empty($option->simg)) ? '<img style="" src="'.$option->simg.'" alt="'.$option->simgalt.'" title="'.$option->simgtitle.'"> ' : '';
			
                    $html[] = '<li style="display:block;height:45px">';
                    $html[] = '<input style="height:36px" type="checkbox" id="' . $id . $i . '" name="'.$control_name.'[' . $name . '][]"' . ' value="'
                    . htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8') . '"' . $checked . '/>';

                    if ($option->showlabel) {
                      $html[] = '<label  style="height:36px;position:relative;top:10px" for="' . $id . $i . '"' . '>' . JText::_($option->text) . '</label>';
                    }
                    
                    $html[] = $simg;
                    $html[] = '</li>';
		
		}   // foreach
				
		$html[] = '</ul>';

		// End the checkbox field output.
		$html[] = '</div>';
		
		// Sorting script - mootools - sort element options with mouse movement.
		$html[] = '<script type="text/javascript">
    
                            window.addEvent("domready", function() {
		
                              new Sortables("div ul#stbul", {
                                 clone: true,
                                 revert: true,
                                 opacity: 0.7
                               });
		
		            }); ';
		$html[] = ' </script> ';

		return implode($html);
		
		} else {
		
		return "";
		
		}  //   if (count($optsIncluded)) {
		
	} 
	
} // class
